package com.loe.ktui

import android.content.Context
import android.view.View

/**
 * ktUI主类
 *
 * @author 章路顺
 * @since 2022/9/30-16:21
 */
object KtUi
{
    private var idCount = 100

    var baseUrl = ""

    /** id映射 */
    private val idMap = HashMap<String, Int>()
    private val nameMap = HashMap<Int, String>()

    fun addId(sId: String, view: View)
    {
        view.id = getId(sId)
    }

    fun getId(sId: String): Int
    {
        var intId = idMap[sId]
        if(intId == null)
        {
            intId = ++idCount
            idMap[sId] = intId
            nameMap[intId] = sId
        }
        return intId
    }

    fun getIdName(id: Int):String
    {
        return nameMap[id] ?: ""
    }

    fun clearId()
    {
        idMap.clear()
        nameMap.clear()
    }

    /** 链接监听 */
    var onLink: (Context.(link: String) -> Unit)? = null
        private set

    /** 设置链接监听 */
    fun setOnLink(onLink: Context.(link: String) -> Unit)
    {
        this.onLink = onLink
    }

    var onImageOpen: (KtImageView.(url: String)->Unit)? = null
        internal set

    /** 设置图片查看事件 */
    fun setOnImageOpen(onOpen: KtImageView.(url: String)->Unit)
    {
        onImageOpen = onOpen
    }

    fun openLink()
    {

    }
}